Nginx

推荐列表 站点导航

当前位置:首页 > 服务器技术 > Nginx >

Nginx下配置Https证书详细过程

来源:网络整理  作者:wy  发布时间:2020-12-23 11:24
这篇文章主要介绍了Nginx下配置Https证书详细过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一...

一、Http与Https的区别

HTTP:是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准(TCP),用于从WWW服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高效,使网络传输减少。

HTTPS:是以安全为目标的HTTP通道,简单讲是HTTP的安全版,即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。HTTPS协议的主要作用可以分为两种:一种是建立一个信息安全通道,来保证数据传输的安全;另一种就是确认网站的真实性。

HTTPS和HTTP的区别主要如下:

1、https协议需要到ca申请证书,一般免费证书较少,因而需要一定费用。

2、http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl加密传输协议。

3、http和https使用的是完全不同的连接方式,用的端口也不一样,前者是80,后者是443。

4、http的连接很简单,是无状态的;HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。

二、使用openssl生成证书

openssl是目前最流行的SSL密码库工具,其提供了一个通用、健壮、功能完备的工具套件,用以支持SSL/TLS协议的实现。

比如生成到:/usr/local/ssl

1

 

openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /usr/local/ssl/nginx.key -out /usr/local/ssl/nginx.crt

 

生成过程:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

 

# openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /u  sr/local/ssl/nginx.key -out /usr/local/ssl/nginx.crt

Generating a 2048 bit RSA private key

...............................................................................+  ++

...............+++

writing new private key to '/usr/local/ssl/nginx.key'

-----

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter '.', the field will be left blank.

-----

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:beijing

Locality Name (eg, city) [Default City]:beijing

Organization Name (eg, company) [Default Company Ltd]:xxxx

Organizational Unit Name (eg, section) []:xxxx

Common Name (eg, your name or your server's hostname) []:xxxx(一般是域名)

Email Address []:[email protected]

# ll

total 8

-rw-r--r--. 1 root root 1391 Apr 21 13:29 nginx.crt

-rw-r--r--. 1 root root 1704 Apr 21 13:29 nginx.key

 

三、Nginx安装http_ssl_module模块

Nginx如果未开启SSL模块,配置Https时提示错误。

nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:xxx

nginx缺少http_ssl_module模块,编译安装的时候带上--with-http_ssl_module配置就行了。

本场景是服务器已经安装过nginx,但是未安装http_ssl_module。

1.进入到源码包,如:

1

 

cd /app/download/nginx-1.12.2

 

2.configure:

1

2

3

4

 

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

 

#可能需要的依赖包

yum -y install pcre-devel openssl openssl-devel

 

3.make:

1

 

make

 

4.不需要执行make install,否则就覆盖安装了。

5.备份原有的nginx,如:

1

 

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak

 

6.然后将刚刚编译好的nginx覆盖掉原有的nginx(nginx需要停止)

1

 

cp ./objs/nginx /usr/local/nginx/sbin/

 

7.查看安装情况:

1

 

/usr/local/nginx/sbin/nginx -V

 

1

2

3

4

5

 

nginx version: nginx/1.12.2

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)

built with OpenSSL 1.0.2k-fips 26 Jan 2017

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

 

四、nginx配置https

贴部分配置信息:

1

2

3

4

5

6

 

server {

 

   listen  80;

       server_name ;

     rewrite ^(.IT之家) https://$server_name$1 permanent; #http 跳转 https

 }

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

 

server {

    listen 443 ssl;

    server_name ;

    ssl_certificate /usr/local/ssl/nginx.crt;

    ssl_certificate_key /usr/local/ssl/nginx.key;

    ssl_session_cache  shared:SSL:1m;

    ssl_session_timeout 5m;

    #禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击

    server_tokens off;

    #如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的浏览器本网站全站加密,并且强制用 HTTPS 访问

    fastcgi_param  HTTPS        on;

    fastcgi_param  HTTP_SCHEME     https;

    access_log /usr/local/nginx/logs/httpsaccess.log;

}

 

先检验配置的对不对:

1

 

/usr/local/nginx/sbin/nginx -t

 

1

2

 

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 

重启nginx:

1

 

/usr/local/nginx/sbin/nginx -s reload

 

访问:

Nginx下配置Https证书详细过程

到此这篇关于Nginx下配置Https证书详细过程的文章就介绍到这了,更多相关Nginx配置Https证书内容请搜索聚合云库文库以前的文章或继续浏览下面的相关文章希望大家以后多多支持聚合云库文库!

原文链接:https://blog.csdn.net/smartdt/article/details/80027579

相关热词: 配置

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/server/nginx/8113.shtml

最新文章
并且强制用 HTTPS 访问 fa 并且强制用 HTTPS 访问 fa

时间:2021-01-14

可以控制访问量 可以控制访问量

时间:2021-01-14

(通配符在前) ③server (通配符在前) ③server

时间:2021-01-14

Nginx环境下WordPress的多站点 Nginx环境下WordPress的多站点

时间:2021-01-05

nginx keepalive的具体使用 nginx keepalive的具体使用

时间:2021-01-05

Nginx的信号控制 Nginx的信号控制

时间:2021-01-05

win10上安装nginx的方法步骤 win10上安装nginx的方法步骤

时间:2020-12-29

linux下 nginx监控问题 linux下 nginx监控问题

时间:2020-12-29

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

Nginx下配置Https证书详细过程

2020-12-23 编辑:wy

一、Http与Https的区别

HTTP:是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准(TCP),用于从WWW服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高效,使网络传输减少。

HTTPS:是以安全为目标的HTTP通道,简单讲是HTTP的安全版,即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。HTTPS协议的主要作用可以分为两种:一种是建立一个信息安全通道,来保证数据传输的安全;另一种就是确认网站的真实性。

HTTPS和HTTP的区别主要如下:

1、https协议需要到ca申请证书,一般免费证书较少,因而需要一定费用。

2、http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl加密传输协议。

3、http和https使用的是完全不同的连接方式,用的端口也不一样,前者是80,后者是443。

4、http的连接很简单,是无状态的;HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。

二、使用openssl生成证书

openssl是目前最流行的SSL密码库工具,其提供了一个通用、健壮、功能完备的工具套件,用以支持SSL/TLS协议的实现。

比如生成到:/usr/local/ssl

1

 

openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /usr/local/ssl/nginx.key -out /usr/local/ssl/nginx.crt

 

生成过程:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

 

# openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /u  sr/local/ssl/nginx.key -out /usr/local/ssl/nginx.crt

Generating a 2048 bit RSA private key

...............................................................................+  ++

...............+++

writing new private key to '/usr/local/ssl/nginx.key'

-----

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter '.', the field will be left blank.

-----

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:beijing

Locality Name (eg, city) [Default City]:beijing

Organization Name (eg, company) [Default Company Ltd]:xxxx

Organizational Unit Name (eg, section) []:xxxx

Common Name (eg, your name or your server's hostname) []:xxxx(一般是域名)

Email Address []:[email protected]

# ll

total 8

-rw-r--r--. 1 root root 1391 Apr 21 13:29 nginx.crt

-rw-r--r--. 1 root root 1704 Apr 21 13:29 nginx.key

 

三、Nginx安装http_ssl_module模块

Nginx如果未开启SSL模块,配置Https时提示错误。

nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:xxx

nginx缺少http_ssl_module模块,编译安装的时候带上--with-http_ssl_module配置就行了。

本场景是服务器已经安装过nginx,但是未安装http_ssl_module。

1.进入到源码包,如:

1

 

cd /app/download/nginx-1.12.2

 

2.configure:

1

2

3

4

 

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

 

#可能需要的依赖包

yum -y install pcre-devel openssl openssl-devel

 

3.make:

1

 

make

 

4.不需要执行make install,否则就覆盖安装了。

5.备份原有的nginx,如:

1

 

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak

 

6.然后将刚刚编译好的nginx覆盖掉原有的nginx(nginx需要停止)

1

 

cp ./objs/nginx /usr/local/nginx/sbin/

 

7.查看安装情况:

1

 

/usr/local/nginx/sbin/nginx -V

 

1

2

3

4

5

 

nginx version: nginx/1.12.2

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)

built with OpenSSL 1.0.2k-fips 26 Jan 2017

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

 

四、nginx配置https

贴部分配置信息:

1

2

3

4

5

6

 

server {

 

   listen  80;

       server_name ;

     rewrite ^(.IT之家) https://$server_name$1 permanent; #http 跳转 https

 }

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

 

server {

    listen 443 ssl;

    server_name ;

    ssl_certificate /usr/local/ssl/nginx.crt;

    ssl_certificate_key /usr/local/ssl/nginx.key;

    ssl_session_cache  shared:SSL:1m;

    ssl_session_timeout 5m;

    #禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击

    server_tokens off;

    #如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的浏览器本网站全站加密,并且强制用 HTTPS 访问

    fastcgi_param  HTTPS        on;

    fastcgi_param  HTTP_SCHEME     https;

    access_log /usr/local/nginx/logs/httpsaccess.log;

}

 

先检验配置的对不对:

1

 

/usr/local/nginx/sbin/nginx -t

 

1

2

 

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 

重启nginx:

1

 

/usr/local/nginx/sbin/nginx -s reload

 

访问:

Nginx下配置Https证书详细过程

到此这篇关于Nginx下配置Https证书详细过程的文章就介绍到这了,更多相关Nginx配置Https证书内容请搜索聚合云库文库以前的文章或继续浏览下面的相关文章希望大家以后多多支持聚合云库文库!

原文链接:https://blog.csdn.net/smartdt/article/details/80027579

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/server/nginx/8113.shtml

相关文章

风云图片

推荐阅读

返回Nginx频道首页